package com.cn.wanxi.servlet.user;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.UserService;
import com.cn.wanxi.service.impl.UserServiceImpl;
import com.cn.wanxi.util.Tool;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/back/user/findAll")
public class UserFindAllServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //     0。乱码处理
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        //        1.得到前端传递过来的数据
        String username = req.getParameter("username");
        String start = req.getParameter("start");
        String end = req.getParameter("end");
        String phone = req.getParameter("phone");
        String sex = req.getParameter("sex");
        String hobby = req.getParameter("hobby");
        UserModel userModel = new UserModel();

        String curPage = req.getParameter("curPage");
        String pageSize = req.getParameter("pageSize");
        PageModel pageModel = new PageModel();
        pageModel.setCurPage(Tool.curPageToInt(curPage));
        pageModel.setPageSize(Tool.pageSizeToInt(pageSize));
        userModel.setPageModel(pageModel);

//          2.封装对象

        userModel.setUsername(username);
        userModel.setStart(start);
        userModel.setEnd(end);
        userModel.setPhone(phone);
        userModel.setSex(sex);
        userModel.setHobby(hobby);

//        调用service
        UserService userService = new UserServiceImpl();
        //        -返回值
        List<UserModel> list = userService.findAll(userModel);
//       反馈给前端--返回值
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("list", list);
        resp.getWriter().println(jsonObject);
    }
}
